﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Main
{
    public partial class CreateDomain : System.Web.UI.Page
    {
        ControlDomains.Control control = new ControlDomains.Control();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ID"] != null)
            {
                if (Session["LIMITEDUSER"] == null)
                {
                    btnVerify.Visible = false; 
                    if (!IsPostBack)
                        ListExistingDomains(control.ViewDomainName(Session["Company"].ToString()));
                }
                else if (Session["LIMITEDUSER"] == "true")
                {
                    Response.Redirect("Default.aspx");
                }
            }
            else
                Response.Redirect("Login.aspx");
        }

        /// <summary>
        /// triggers listing of existing domains on SQL Azure
        /// </summary>
        /// <param name="list"></param>
        private void ListExistingDomains(List<VerifiedDomain> list)
        {
            TechnoStoreMainDataContext db = new TechnoStoreMainDataContext();
            var query = from u in db.VerifiedDomains
                        orderby u.DomainName ascending
                        where u.DomainName != String.Empty && u.Company == Session["Company"].ToString()
                        select u; 
            
            foreach (var Result in query)
            {
                if (Result == null)
                {
                    txtExistingDomain.Text = "No existing domains";
                    break;
                }
                else
                    txtExistingDomain.Text = txtExistingDomain.Text + Result.DomainName + " ; ";
            }
        }

        /// <summary>
        /// triggers creation of new domain on Office 365 and SQL Azure
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                verifyDiv.InnerHtml = "1.Sign in to your domain registrar’s website, and then select the domain that you’re verifying." +
                     "<br>2.In the DNS management area for your account, choose the option to add a TXT record for your domain." +
                     "<br>3.In the TXT box for the domain, type the following: @" +
                     "<br>4.In the Fully qualified domain name (FQDN) or Points to box, type the following: <b>MS=" + control.CreateDomainName(txtDomainName.Text.Trim().ToLower(), Session["Company"].ToString()) + "</b>" +
                     "<br>5.Where it asks for TTL information, type the following: 1 Hour" +
                     "<br>6.Save your changes, and then sign out of your domain registrar’s website. Wait at least 15 minutes for this change to take effect.";
                btnVerify.Visible = true;
                ListExistingDomains(control.ViewDomainName(Session["Company"].ToString()));
            }
            catch (Exception ex)
            {
                lblError.Text = ex.ToString();
            }
        }

        /// <summary>
        /// makes all fields blank
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtDomainName.Text = String.Empty;
            lblError.Text = String.Empty;
        }

        /// <summary>
        /// triggers DNS verification of newly created domain
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnVerify_Click(object sender, EventArgs e)
        {
            try
            {
                if (control.VerifyDomainOn365(txtDomainName.Text.Trim().ToLower(), Session["Company"].ToString()) == "true")
                {
                    lblVerify.Text = "Domain name has been successfully verified!";
                    verifyDiv.InnerHtml = String.Empty;
                    lblError.Text = String.Empty;
                }
                else
                {
                    lblError.Text = "Verification failed! Try again!";
                    btnVerify.Visible = true;
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.ToString();
                btnVerify.Visible = true;
            }
        }
    }
}